package gui.components;

import javax.swing.JTextField;

public class CalcTextField extends JTextField {

	private static final long serialVersionUID = -7745002854013620482L;
	private boolean numberTyped  = true;
	private boolean precalculated = false;
	
	public CalcTextField()
	{
		super();
	}

	public CalcTextField(String text)
	{
		super(text);
	}

	public void typed()
	{
		setTyped(true);
	}
	
	public void setTyped(boolean flag)
	{
		numberTyped = flag;
	}
	
	public boolean isTyped()
	{
		return numberTyped;
	}
	
	public boolean isPrecalculated()
	{
		return precalculated;
	}

	public void setPrecalculated(boolean precalculated)
	{
		this.precalculated = precalculated;
	}

	public void readSymbol(char symbol)
	{
		String str = getText();
		setPrecalculated(false);
		if (numberTyped)
		{
			str = "0";
			numberTyped = false;
		}
		
		int numberOfDigits = str.length();
		if (str.contains("."))
		{
			numberOfDigits--;
		}
		if (str.contains("-"))
		{
			numberOfDigits--;
		}
		switch (symbol)
		{
			case '.':
				if (numberOfDigits >= 10)
				{
					break;
				}
				if (!str.contains("."))
				{
					str += symbol;
				}
				break;
			case '0':
				if (numberOfDigits >= 10)
				{
					break;
				}
				if (!(str.length() == 1 && str.charAt(0) == '0'))
				{
					str += symbol;
				}
				break;
			case 'P':
				str = Double.toString(Math.PI).substring(0, 10);
				numberTyped = true;
				break;
			default:
				if (str.length() >= 10)
				{
					break;
				}
				if (str.equals("0"))
				{
					str = "" + symbol;
				}
				else
				{
					str += symbol;
				}
				break;
		}
		setText(str);
	}
	
}
